# -*- coding: utf-8 -*-
"""
@Env 
@Time 2024/9/10 上午8:59
@Author yzpang
@Function: 模型加载测试
"""
import os
import time
from multiprocessing import Process, set_start_method

from modelserver.configs.model_config import MODEL_ROOT_PATH
import paddle


def load_pre_model(model_path):
    from paddlenlp.transformers import AutoModelForSequenceClassification, AutoTokenizer
    device = paddle.device.get_device()
    print(device)
    try:
        print(paddle.in_dynamic_mode())
        paddle.disable_static()
        model = AutoModelForSequenceClassification.from_pretrained(model_path)
        while True:
            time.sleep(1)
    except Exception as e:
        print(f"Exception: {e}")


def load_sub_process(model_path):
    print(paddle.in_dynamic_mode())
    set_start_method('spawn')
    process = Process(target=load_pre_model, args=(model_path,))
    process.start()
    process.join()


def load_model():
    model_path = os.path.join(MODEL_ROOT_PATH, "BERT/bert-wwm-chinese")
    load_sub_process(model_path)


if __name__ == '__main__':
    # device = os.environ["CUDA_VISIBLE_DEVICES"]
    # print(device)
    load_model()
    while True:
        time.sleep(1)
